﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV.Public;
using QLTV.BLL;

namespace QLTV.GUI.Forms
{
    public partial class frmSach : DevExpress.XtraEditors.XtraForm
    {
        DauSachBLL dsBLL = new DauSachBLL();
        DauSachDTO dsDTO = new DauSachDTO();

        SachDTO sDTO = new SachDTO();
        SachBLL sBLL = new SachBLL();

        string tam = "";
        private bool kt = false;

        public frmSach()
        {
            InitializeComponent();
            DataTable tb = dsBLL.DauSach_TraCuu();
            gridDS.Properties.DataSource = tb;
            gridDS.Properties.DisplayMember = "TenDS";
            gridDS.Properties.ValueMember = "MaDS";      
            

        }
        public frmSach(string _mas)
        {
            InitializeComponent();

            sDTO.MaSach = _mas;
            DataTable dt = sBLL.LoadDuLieuCotMaSach(sDTO);
            tam = dt.Rows[0]["MaSach"].ToString();

            //string mads_tam = dt.Rows[0]["MaDS"].ToString();

            DataTable tb = dsBLL.DauSach_TraCuu();
            gridDS.Properties.DataSource = tb;
            gridDS.Properties.DisplayMember = "TenDS";
            gridDS.Properties.ValueMember = "MaDS";

            //string kkk = dt.Rows[0]["TenDS"].ToString();
            //gridDS.EditValue = kkk;
            
            
            

            txtNam.Text = dt.Rows[0]["NamXB"].ToString();

            txtNXB.Text = dt.Rows[0]["TenNXB"].ToString();

            txtTG.Text = dt.Rows[0]["TenTG"].ToString();

            txtTGia.Text = dt.Rows[0]["TriGia"].ToString();

            txtTL.Text = dt.Rows[0]["TenTL"].ToString();
            
        }

        private void gridDS_EditValueChanged(object sender, EventArgs e)
        {
            if (gridDS.Properties.View.GetFocusedRowCellValue("TenDS") != null)
            {
                dsDTO.TenDS = gridDS.Properties.View.GetFocusedRowCellValue("TenDS").ToString();
                 
                DataTable tb = dsBLL.timDauSachDayDu(dsDTO);
                txtTL.Text = tb.Rows[0]["TenTL"].ToString();
                txtTG.Text = tb.Rows[0]["TenTG"].ToString();
                txtNXB.Text = tb.Rows[0]["TenNXB"].ToString();
                txtNam.Text = tb.Rows[0]["NamXB"].ToString();
                txtTGia.Text = tb.Rows[0]["TriGia"].ToString();
            }
        }

        private void btnGhiDuLieu_Click(object sender, EventArgs e)
        {
            try
            {
                if (tam == "")
                {
                    sDTO.MaSach = "";
                }
                else
                {
                    sDTO.MaSach = tam;
                }
                sDTO.MaDS = ("" + gridDS.Properties.View.GetFocusedRowCellValue("MaDS").ToString().Trim());
                sDTO.TinhTrang = true;
                if (gridDS.Text.Trim() == "")
                {
                    MessageBox.Show("Bạn phải chọn tên đầu sách", "Cảnh báo thiếu!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    int kq = sBLL.nhapSach(sDTO);
                    if (kq < 0)
                    {
                        kt = true;
                        Is_Send = true;
                        if (tam == "")
                        {
                            gridDS.Focus();
                            MessageBox.Show("Đã thêm thành công");

                        }
                        else
                        {
                            MessageBox.Show("Đã cập nhật dữ liệu thành công");
                            this.Close();
                            tam = "";
                        }
                    }
                    else
                    {
                        MessageBox.Show("Thêm thất bại");
                    }
                    gridDS.Text = "";
                    txtNam.Text = "";
                    txtNXB.Text = "";
                    txtTG.Text = "";
                    txtTGia.Text = "";
                    txtTL.Text = "";
                }
            }
            catch 
            {
                MessageBox.Show("Bạn phải chọn tên đầu sách", "Cảnh báo thiếu!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        public delegate void MyDelegate(object sender, bool check);
        public event MyDelegate myEvent;
        private bool Is_Send
        {
            set
            {
                if (value)
                {
                    if (myEvent != null) myEvent(sender: this, check: kt);
                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAddDS_Click(object sender, EventArgs e)
        {
            frmDauSach frmDS = new frmDauSach();

            frmDS.myEvent += frm_myEvent;

            frmDS.ShowDialog();
        }
        private void frm_myEvent(object sender, bool check)
        {
            bool kt = check;
            if (kt == true)
                gridDS.Properties.DataSource = dsBLL.DauSach_TraCuu();
        }
    }
}